LESSON4:

    If.. Then.. Elsestatements allow the program to branch off in many different directions. ASM does not support these staments directly, but using CP along with JR/JPyou can acheive the effect.
 
Theseare the instructions/ideas that will be covered in this lesson:
CP
IF... THEN...ELSE
 
CP
  CP simply setsthe FLAG register (F) based on the subraction (A-s) back to list of instructions

 IF... THEN... ELSE
  To acheivean IF... THEN... ELSE statement(s) you must use a CP call followed by aJR, JP, or any other instruction that has conditionals (ret, call, ...) Here are some examples:
  ld b,5       ;b=5
  ld a,5       ;a=5
 
  cp b         ;set flags based on (a-b)
  jr z,AequalsB ;if a=b -> a-b=0,therefore
               ;if the Zero flag is set JUMP to AequalsB
  :
  .
AequalsB: ;
  :
  .
  You can also string them together like this:
  ld b,6  ;.
  ld c,5  ;.
  ld a,5  ;Set the init values
 
  cp b    ;IF a=b
  jr z,AB ;THEN goto AB
  cp c    ;IF a=c
  jr z,AC ;THEN goto AC
  jr NONE ;ELSE goto NONE (if it didn'tjump one of the other
         ; times then it must be an ELSE, NOTE:no conditia
         ; listed.  Just a straight 'jr NONE')
:
.
AB:

:
.
AC:
:
.
NONE:

  You've seen how to test for equality.  now forGREATER THAN, and LESS THAN:
ld b,7     ;.
ld a,3     ;Set up values

cp b              ;set flags based on (A-B)
jp p,AGreaterThanB ;IF (a-b) is positivethen a>b
jp m,BGreaterThanA ;IF (a-b) is minus(negative) then b>a
jp AEqualsB       ;ELSE a=b
:
:
.

back to list of instructions
LESSON3     INDEX     LESSON5
This is the end of the lesson, I do not gaurantee any correctnessof any statements made above.
1